package com.ijiuchuang.jc_localization.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.app.Config;
import com.ijiuchuang.jc_localization.bean.lite.Map;
import com.ijiuchuang.jc_localization.bean.lite.Setts;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.OnItemClickListener;
import com.ijiuchuang.jc_localization.view.activity.FragmentActivity;
import com.ijiuchuang.jc_localization.view.adapter.MapListItemTouchListener;
import com.ijiuchuang.jc_localization.view.adapter.SettsMapListRecyclerAdapter;
import com.litesuits.orm.db.assit.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettsMapListFragment extends Fragment {

    @BindView(R.id.setts_map_list_recycler)
    RecyclerView mRecyclerView;
    @BindView(R.id.map_list_fab)
    FloatingActionButton fab;
    @BindView(R.id.map_list_frame)
    FrameLayout frameView;

    private SettsMapListRecyclerAdapter mRecyclerAdapter;

    public SettsMapListFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_setts_map_list, container, false);
        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();

        List<Map> list = Config.liteOrm.query(Map.class);
        // 默认地图不可删
        long defaultPosition = 1;

        QueryBuilder<Setts> equals = new QueryBuilder<>(Setts.class)
                .whereEquals(Setts.COL_KEY, Setts.SETTINGS_MAP_SCENE);
        ArrayList<Setts> list1 = Config.liteOrm.query(equals);
        if (!list1.isEmpty()) {
            defaultPosition = list1.get(0).getValueLong();
            // TODO: 2016/6/25 下标越界
        }

        mRecyclerAdapter = new SettsMapListRecyclerAdapter(getActivity(), R.layout.recycler_item_setts_map_list, list);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mRecyclerAdapter);

        mRecyclerAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Object o, int position) {
                Intent intent = new Intent(getActivity(), FragmentActivity.class);
                intent.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTS_MAP_DETAIL);
                Bundle bundle = new Bundle();
                bundle.putSerializable(SettsMapDetailFragment.BUNDLE_MAP, (Map) o);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

        ItemTouchHelper helper = new ItemTouchHelper(new MapListItemTouchListener(mRecyclerAdapter, frameView, fab, defaultPosition));
        helper.attachToRecyclerView(mRecyclerView);
    }

    @OnClick(R.id.map_list_fab)
    public void onClick() {
        Intent intent = new Intent(getActivity(), FragmentActivity.class);
        intent.putExtra(FragmentActivity.FRAGMENT_NAME, FragmentActivity.FRAGMENT_NAME_SETTS_MAP_DETAIL);
        startActivity(intent);
    }
}
